import { HttpClient, HttpEventType, HttpRequest } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { res } from 'src/common/url.service';

@Component({
  selector: 'app-water-mark',
  templateUrl: './water-mark.component.html',
  styleUrls: ['./water-mark.component.less'],
})
export class WaterMarkComponent implements OnInit {
  constructor(private http: HttpClient, private message: NzMessageService) {}

  ngOnInit(): void {}
  current = 0;
  next1() {
    if (this.FileList.length <= 0) {
      this.message.error('请添加文件！');
      return;
    }
    const form = new FormData();
    this.FileList.map((file) => {
      form.append('files[]', file);
    });
    this.http
      .post(`http://127.0.0.1:7001/pdf/watermark`, form)
      .subscribe(({ code, data, msg }: any) => {
        if (code == 200) {
          this.current = 1;
        }
      });
  }
  dwonloadPercent = 0;
  download(evt) {
    evt.preventDefault();

    this.http
      .get(`http://127.0.0.1:7001/pdf/downloadAll`)
      .subscribe((res: res) => {
        if (res.code == 200) {
          this.http
            .request(
              new HttpRequest('GET', res.data, {
                reportProgress: true,
                responseType: 'blob',
              })
            )
            .subscribe((res) => {
              switch (res.type) {
                case HttpEventType.DownloadProgress: {
                  this.dwonloadPercent = Math.round(
                    (res.loaded / res.total) * 100
                  );
                  break;
                }
                case HttpEventType.Response: {
                  const url = URL.createObjectURL(res.body);
                  const a = document.createElement('a');
                  a.href = url;
                  a.download = 'PDF.zip';
                  a.click();
                  this.dwonloadPercent = 0;
                }
              }
            });
        }
      });
  }

  FileList = [];

  beforeUploadpdf = (file) => {
    this.FileList = this.FileList.concat(file);
    return false;
  };
  getAddPDFAndJC() {
    this.http
      .get(`http://127.0.0.1:7001/pdf/AddPDFAndJC`, {
        responseType: 'blob',
      })
      .subscribe((res) => {
        const url = URL.createObjectURL(res);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'AddPDFAndJC.txt';
        a.click();
      });
  }
}
